$(function() {
	$(".mo-content>ul").sortable({
		containment : ".mo-content",
		opacity : 0.1,
		cursorAt : {
			top : 60,
			left : 70
		},
		// 排序好事件更新
		update : function(event, ui) {
			var pptnums = $(".mo-content>ul li").length;
			// 更新PPT前面的页数提醒
			for ( var i = 0; i < pptnums; i++) {
				$(".mo-content li:eq(" + i + ") .q-num").html(i + 1);
			}

			pptOrderChange(undefined);
		}
	});

	$(".mo-content>ul>li").disableSelection();

	$(".mo-content>ul>li>.q-panel").mouseover(function() {
		$(this).addClass("border");
	}).mouseleave(function() {
		$(this).removeClass("border");
	});
});

/**
 * 如果是删除，其后所有数值都默认-1;
 * 
 */
function pptOrderChange(deleNO) {
	// 重新扫描下面li的id排列顺序
	var liOrders = new Array();// 下标也从1开始
	var lis = $(".mo-content>ul li");
	for ( var i = 0; i < lis.length; i++) {
		li_id = lis[i].id;// li-0 li-1
		liOrders[i + 1] = parseInt(li_id.split("-")[1]);
	}

	// 遍历是否有跳转信息
	for ( var j = 1; j < liOrders.length; j++) {
		var li_id = liOrders[j];
		var contentStr = ppt.contentA[li_id];
		var right_jump_to = "none";
		var wrong_jump_to = "none";
		var new_right_jump_to= "none";
		var new_wrong_jump_to= "none";

		// 正确跳转信息
		var right_start_index = contentStr.indexOf("right-jump-to=");
		if (right_start_index != -1) {
			right_start_index = contentStr.indexOf("\"", right_start_index);
			var right_end_Index = contentStr.indexOf("\"",
					right_start_index + 1);
			right_jump_to = contentStr.substring(right_start_index + 1,
					right_end_Index);
			if (right_jump_to != "none") {
				//由于删除了PPT，后面的都无形往前移了一格
				//console.info("deleNO:"+deleNO+" right_jump_to:"+right_jump_to);
				if(deleNO!=undefined && right_jump_to>deleNO){
					alert("enter");
					right_jump_to = parseInt(right_jump_to);
					new_right_jump_to = right_jump_to;
				}
				else{
					right_jump_to = parseInt(right_jump_to) + 1;
					// 变更顺序后,找到要原先要跳转的PPT的index(index 从1开始)
					new_right_jump_to = liOrders
							.indexOf(ppt.lastPPTOrders[right_jump_to]);
				}
				//console.info("new_right_jump_to:" + new_right_jump_to);

				// 更新ppt中contentA中的 jump_to下标从0开始
				updatePPTContentA(li_id, new_right_jump_to - 1, undefined);
			}
		}

		// 错误跳转信息
		var wrong_start_index = contentStr.indexOf("wrong-jump-to=");
		if (wrong_start_index != -1) {
			wrong_start_index = contentStr.indexOf("\"", wrong_start_index);
			var wrong_end_Index = contentStr.indexOf("\"",
					wrong_start_index + 1);
			wrong_jump_to = contentStr.substring(wrong_start_index + 1,
					wrong_end_Index);
			if (wrong_jump_to != "none") {
				if(deleNO!=undefined && wrong_jump_to>deleNO){
					wrong_jump_to = parseInt(wrong_jump_to);
					new_wrong_jump_to = wrong_jump_to;
				}
				else{
					wrong_jump_to = parseInt(wrong_jump_to) + 1;
					// 变更顺序后,找到要原先要跳转的PPT的index(index 从1开始)
					new_wrong_jump_to = liOrders
							.indexOf(ppt.lastPPTOrders[wrong_jump_to]);
				}

				console.info("new_wrong_jump_to:" + new_wrong_jump_to);
				// 更新ppt中contentA中的
				updatePPTContentA(li_id, undefined, new_wrong_jump_to - 1);
			}
		}
	}

	// 由于手动更新了contentA中的内容，重新加载当前PPT
	$(".c-content").html(ppt.contentA[ppt.curNO]);
	// 重新绑定事件
	rebindListener();
	initPointLaout();

	for ( var i = 1; i < ppt.lastPPTOrders.length; i++) {
		//console.info("lastPPTOrders:" + i + " " + ppt.lastPPTOrders[i]);
	}
	for ( var i = 1; i < liOrders.length; i++) {
		//console.info("liOrders:" + i + " " + liOrders[i]);
	}

	for ( var i = 1; i <= liOrders.length; i++) {
		if (liOrders[i] == undefined) {
			break;
		}
		ppt.lastPPTOrders[i] = liOrders[i];
	}
}

/**
 * 更新PPT中对应contentA中right_jump_to 和 wrong_jump_to 中的值
 * 
 * @param li_id
 */
function updatePPTContentA(li_id, new_right_jump_to, new_wrong_jump_to) {
	var contentStr = ppt.contentA[li_id];
	if (contentStr == undefined) {
		return;
	}

	if (new_right_jump_to != undefined
			&& contentStr.indexOf("right-jump-to") != -1) {
		//console.info("1:"+contentStr);
		contentStr = contentStr.replace(new RegExp("right-jump-to=\".*?\"",
				"gm"), "right-jump-to=\"" + new_right_jump_to + "\"");
		//console.info("2:"+contentStr);
	}

	if (new_wrong_jump_to != undefined
			&& contentStr.indexOf("wrong-jump-to") != -1) {
		// console.info("1:"+contentStr);
		contentStr = contentStr.replace(new RegExp("wrong-jump-to=\".*?\"",
				"gm"), "wrong-jump-to=\"" + new_wrong_jump_to + "\"");
		// console.info("2:"+contentStr);
	}
	ppt.contentA[li_id] = contentStr;

}
